#include "login.h"
//#include "mainwindow.h"
#include "register.h"
#include "ui_login.h"
//#include "ui_mainwindow.h"
#include "userconf.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QFile>
#include <QDebug>
#include <QStringList>
#include <QString>
#include "browserview.h"

QString ACCOUNT = QString();

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    ui->psw->setEchoMode(QLineEdit::Password);

    setWindowTitle("用户登录");
    //setFixedSize(486,305);

    connect(ui->login,&QPushButton::clicked,this,&Login::on_login_clicked);
    connect(ui->register_2,&QPushButton::clicked,this,&Login::on_register_clicked);
//    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
//    db.setHostName("127.0.0.1");
//    db.setUserName("root");
//    db.setPassword("123456");
//    db.setDatabaseName("mysql");

//    if(!db.open())
//    {
//        QMessageBox::critical(0,"错误","无法连接(mark)",QMessageBox::Yes);
//    }


}

void Login::on_login_clicked()
{
    QString acc = ui->account->text();
    QString psw = ui->psw->text();

    if(acc == "" ||psw == ""){
            QMessageBox::information(this,"警告","输入不能为空",QMessageBox::Ok);
        }else{

            QSqlQuery query;
            query.prepare("select account,psw from user where account=:acc and psw = :psw ");

            query.bindValue(":acc",acc);
            query.bindValue(":psw",psw);
            query.exec();
            if(!query.next())
            {
                QMessageBox::information(this,"警告","用户名或密码错误！",QMessageBox::Ok);
            }
            else
            {
                QMessageBox::information(this,"提醒","登录成功！",QMessageBox::Ok);

                ACCOUNT.append(acc);

//                mwui->btn_user->setText("");
//                mwui->btn_user->disconnect();

//                UserConf *userconf = new UserConf;
//                connect(mwui->btn_user,&QPushButton::clicked,userconf,&MainWindow::show);
                //this->parentWidget()->close();
                //this->parentWidget()->parentWidget()->close();
                emit closewin();
                this->close();
            }
        }

}

void Login::on_register_clicked()
{
    Register * rg = new Register;
    rg->show();
}

Login::~Login()
{
    delete ui;
}
